package cn.joytur.modules.order.entites;

import java.util.List;

import org.apache.http.client.utils.DateUtils;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.joytur.common.annotation.TabMapping;
import cn.joytur.common.mvc.constant.DictAttribute;
import cn.joytur.common.mvc.dto.Sort;
import cn.joytur.common.utils.JoyDictUtil;
import cn.joytur.modules.order.entites.base.BaseRechargeOrder;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
@TabMapping(tabName = "joy_recharge_order")
public class RechargeOrder extends BaseRechargeOrder<RechargeOrder> {
	public static final RechargeOrder dao = new RechargeOrder().dao();
	
	/**
	 * 分页查询
	 */
	public Page<RechargeOrder> paginate(int pageNumber, int pageSize, RechargeOrder query, Sort sort){
		SqlPara sqlPara = getSqlPara("rechargeOrder.findListPage", Kv.by("query", query));
		return paginate(pageNumber, pageSize, sqlPara);
	}
	
	/**
	 * 更新所有过时订单
	 * @return
	 */
	public int updateAllExpireOrder(){
		Long updateStatus = Long.valueOf(JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_COLSE, DictAttribute.RECHARGE_ORDER_STATUS, "0"));
		Long whereStatus = Long.valueOf(JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_UNPAY, DictAttribute.RECHARGE_ORDER_STATUS, "1"));
		SqlPara sqlPara = getSqlPara("rechargeOrder.updateAllExpireOrder", updateStatus, whereStatus);
		return Db.update(sqlPara);
	}
	
	/**
	 * 更新用户过时订单
	 * @return
	 */
	public int updateMemberAllExpireOrder(String wechatMemberId){
		Long updateStatus = Long.valueOf(JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_COLSE, DictAttribute.RECHARGE_ORDER_STATUS, "0"));
		Long whereStatus = Long.valueOf(JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_UNPAY, DictAttribute.RECHARGE_ORDER_STATUS, "1"));
		SqlPara sqlPara = getSqlPara("rechargeOrder.updateMemberAllExpireOrder", updateStatus, whereStatus, wechatMemberId);
		return Db.update(sqlPara);
	}
	
	/**
	 * 查询今日充值成功金额
	 * @return
	 */
	public Integer findCountByToDay(){
		SqlPara sqlPara = getSqlPara("rechargeOrder.findCountByToDay");
		String status = JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_FINISH, DictAttribute.RECHARGE_ORDER_STATUS, "3");
		String today = DateUtil.format(new java.util.Date(), DatePattern.NORM_DATE_PATTERN) + " 00:00:00";
		return Db.queryInt(sqlPara.getSql(), status, today);
	}
	
	/**
	 * 分页查询
	 */
	public List<RechargeOrder> findWaitHandleRechargeOrderList(){
		String status0 = JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_COLSE, DictAttribute.RECHARGE_ORDER_STATUS, "0");
		String status1 = JoyDictUtil.getDictValue(DictAttribute.RECHARGE_ORDER_STATUS_UNPAY, DictAttribute.RECHARGE_ORDER_STATUS, "1");
		String ctime = DateUtils.formatDate(DateUtil.offsetDay(new java.util.Date(), -1), DatePattern.NORM_DATETIME_PATTERN);
		SqlPara sqlPara = getSqlPara("rechargeOrder.findWaitHandleRechargeOrderList", status0, status1, ctime);
		return find(sqlPara);
	}
	
}
